const api = require("../../../ws_micro_shop/utils/api.js");
const app = getApp();

Page({
  data: {
    status: 1,
    page: 1,
    first_count: 0,
    second_count: 0,
    third_count: 0,
    list: [],
    theme: app.themeData,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: "暂无分销商，快去发展分销商吧~"
    },
    sort_list: [
      {
        name: "消费额"
      },
      {
        name: "订单"
      },
      {
        name: "推广人数"
      },
      {
        name: "加入时间"
      }
    ],
    sort: 0,
    isOpen: false,
    isShowLoading: false,
    imgUrl: app.imgUrl,
    is_show_line: false,
  },
  onLoad(options) {
    let that = this;
    wx.setNavigationBarTitle({
      title: "我的团队"
    });
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit
      });
    });
    let share_setting = wx.getStorageSync("share_setting");
    this.setData({
      share_setting: share_setting
    });
    this.GetList(options.status || 1);
  },
  switchlist(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      status: parseInt(status || 1, 10),
      page: 1,
      list: [],
      isShowLoading: false,
      is_show_line: false
    });
    this.GetList(status);
  },
  onReachBottom() {
    let status = this.data.status;
    this.GetList(status);
  },
  GetList(status) {
    let that = this;
    let { page, list, sort } = that.data;
    wx.showLoading({
      title: "正在加载...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.share.get_team,
      data: {
        status,
        page,
        sort
      },
      success: function(res) {
        if (res.code === 0) {
          if (res.data.list.length > 0) {
            page += 1;
            that.setData({
              first_count: res.data.first,
              second_count: res.data.second,
              third_count: res.data.third,
              list: list.concat(res.data.list),
              page
            });
          } else {
            // wx.showToast({
            //   title: '没有更多了',
            //   icon: 'none',
            // });
            that.setData({
              is_show_line: page > 1 ? true : false,
            })
          }
        } else {
          wx.showToast({
            title: res.msg || "加载失败",
            icon: "none"
          });
        }
      },
      complete: function() {
        wx.hideLoading();
        that.setData({
          isShowLoading: true,
        })
      }
    });
  },

  openSort() {
    let { isOpen } = this.data;
    this.setData({
      isOpen: !isOpen
    });
  },

  selectSort(e) {
    let { index } = e.currentTarget.dataset;
    this.setData({
      sort: index,
      isOpen: false,
      list: [],
      page: 1,
      isShowLoading: false,
      is_show_line: false,
    });
    this.GetList(1);
  },

  collectFormId(e) {
    app.collectFormId(e);
  }
});
